﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace SuperInterMarket.Mail
{
    public partial class MailConfig : Form
    {
        public MailConfig()
        {
            InitializeComponent();
        }

        private void DefaultOkBt_Click(object sender, EventArgs e)
        {
            string sendmodel = "onetoone";
            if (OneToOneRadio.Checked)
            {
                sendmodel = "onetoone";
            }
            else
            {
                sendmodel = "onetomore";
            }
            int sendNum = 1;
            string sendNumStr = SendNumTxt.Text;
            if (string.IsNullOrEmpty(sendNumStr))
            {
                MessageBox.Show("发送封数为空，默认为1");                
            }
            else
            {
                sendNum = int.Parse(sendNumStr);
            }

            bool isSsl = SSLCheckBox.Checked;
            bool flag = true;
            try
            {
                XDocument doc = XDocument.Load(SysConstants.MailConfigFile);
                XElement config = doc.Element("config");
                config.SetElementValue("sendmodel", sendmodel);
                config.SetElementValue("sendnum", sendNum);
                config.SetElementValue("isssl", isSsl==true?1:0);
                doc.Save(SysConstants.MailConfigFile);
            }
            catch (Exception)
            {
                flag = false;
                throw;
            }
            MessageBox.Show(flag==true?"保存成功":"保存失败");
            //doc.Save();
            this.Close();
        }

        private void DefaultFrom_Load(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load(SysConstants.MailConfigFile);
            XElement config = doc.Element("config");
            if (!config.Element("sendmodel").IsEmpty)
            {
                string sendmodel = config.Element("sendmodel").Value;
                if (sendmodel.Equals("onetoone"))
                {
                    OneToOneRadio.Checked = true;
                }
                else
                {
                    OneToMoreRadio.Checked = true;
                }
               
            }
            if (!config.Element("sendnum").IsEmpty)
            {
                
                SendNumTxt.Text = config.Element("sendnum").Value;
            }
            if (!config.Element("isssl").IsEmpty)
            {
                string isssl = config.Element("isssl").Value;
                if (isssl.Equals("1"))
                {
                    SSLCheckBox.Checked = true;
                }
                else
                {
                    SSLCheckBox.Checked = false;
                }
                
            }
            
            
        }

        private void DefaultCancelBt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
